package com.zysc.processflow.controller;

import com.zysc.processflow.entity.SpecGroup;
import com.zysc.processflow.entity.SpecParam;
import com.zysc.processflow.service.impl.BrandService;
import com.zysc.processflow.service.impl.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Mr.Liuyijiang
 * @Description:
 * @Date Created in 2022-06-08 22:17
 * @Modified By:
 */
@Controller
@RequestMapping("/item/spec")
public class SpecificationController  {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 新增参数
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParam specParam){
        this.specificationService.saveParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 删除参数
     * @return
     */
    @GetMapping("deleteparam")
    public ResponseEntity<Void> deleteParam(String id){
        this.specificationService.deleteParam(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    /**
     * 删除参数组
     * @return
     */
    @GetMapping("deletegroup")
    public ResponseEntity<Void> deletegroup(String id){
        this.specificationService.deletegroup(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 新增参数组
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroup specGroup){
        this.specificationService.saveGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){

        List<SpecGroup> groups = this.specificationService.queryGroupsByCid(cid);
        if(CollectionUtils.isEmpty(groups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }

    /**
     * 根据条件查询规格参数
     * @param cid
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "searching",required = false) Boolean searching
            ){

        List<SpecParam> params = this.specificationService.queryParams(gid,cid,generic,searching);
        if(CollectionUtils.isEmpty(params)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(params);
    }
}
